document.querySelector("#btn-login").addEventListener("click", async () => {
    // axios({
    //     URL: "/register",
    //     method: "post",
    //     data: {
    //         username: "jack",
    //         password: "123456"
    //     }
    // })

    //搜集数据

    const form = document.querySelector(".login-form")
    const data = serialize(form, { hash: true, empty: true })
    console.log('data:', data);

    const { username, password } = data

    //非空检验
    if (username === "" || password === "") {
        showToast("用户名或密码不能为空")
        return
    }

    //长度校验
    if (username.length < 3 || username.length > 30 || password.length < 3 || password.length > 30) {
        showToast("用户名长度必须在3-30之间,密码长度必须在3-30之间")
        return
    } //数据提交
    // try {
        const res = await axios.post("/login", { username, password })
        // console.log(res);
        showToast(res.data.message);

        // 缓存数据
        localStorage.setItem("username", res.data.username)
        localStorage.setItem("token", res.data.token)
        
        //去主页
        setTimeout(() => {
            window.location.href = "./index.html"
        }, 1500)
    // } catch (error) {
    //     showToast(error.response.data.message)
    // }
})